Copyright (C) 2020 Andreas Kloeckner
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import numpy as np
import numpy.linalg as la
np.set_printoptions(precision=3, linewidth=120)
Let's prepare a matrix with some random or deliberately chosen eigenvalues:
n = 6
if 1:
np.random.seed(70)
eigvecs = np.random.randn(n, n)
eigvals = np.sort(np.random.randn(n))
# Uncomment for near-duplicate largest-magnitude eigenvalue
# eigvals[1] = eigvals[0] + 1e-3
A = eigvecs.dot(np.diag(eigvals)).dot(la.inv(eigvecs))
print(eigvals)
else:
# Complex eigenvalues
np.random.seed(40)
A = np.random.randn(n, n)
print(la.eig(A)[0])
Let's also pick an initial vector:
x = x0
Now implement plain power iteration.
Back to the beginning: Reset to the initial vector.
x0 = np.random.randn(n)
x = x0
Implement normalized power iteration.
x = x0
errors = []
coeffs = la.solve(eigvecs, x0)
for i in range(10):
x = A @ x
errors.append(
la.norm(x/eigvals[0]**(i+1) - coeffs[0]*eigvecs[:,0], 2))
print("coefficients:", la.solve(eigvecs, x/la.norm(x,2)))
conv_factor = eigvals[1]/eigvals[0]
errors = np.array(errors)
for i in range(len(errors)-1):
print(f"{i=}: {errors[i]=:.6e}, {errors[i+1]/errors[i]=:.6g}, {conv_factor=:.6g}")
What if we want the eigenvalue closest to a give value $\sigma$?
Once again, reset to the beginning.
x = x0/la.norm(x0)
Can we feed an estimate of the current approximate eigenvalue back into the calculation? (Hint: Rayleigh quotient)
Reset once more.
x = x0/la.norm(x0)
Run this cell in-place (Ctrl-Enter) many times.